<?php


namespace app\teacher\controller;

use app\teacher\utils\User as AdminUser;
use think\Db;

class Sign extends Base
{
    public function index()
    {
        $page = request()->param('page', '1', 'intval');
        $limit = request()->param('limit', 10, 'intval');
        $student_no = request()->param('student_no', '');
        $realname = request()->param('realname', '');
        $zhuanye = request()->param('zhuanye', '');
        $class = request()->param('class', '');
        $grade = request()->param('grade', '');
        $start_and_end = request()->param('start_and_end');
        $uid = AdminUser::getUid();
        $user_type = $this->user_type();
        // 教师所管的学生id
        if($user_type == 1){//辅导员
            $student_ids = model('Member')->where('fdy', $uid)->column('uid');
        }else{
            $xueyuan_id = Db::name('yuan')->where('yuanzhang',$uid)->value('id');
            $student_ids = model('Member')->where('xueyuan', $xueyuan_id)->column('uid');
        }
        $where[] = ['s.uid', 'in', $student_ids];
        if ($student_no) {
            $where[] = ['student_no', 'like', '%' . $student_no . '%'];
        }
        if ($realname) {
            $where[] = ['realname', 'like', '%' . $realname . '%'];
        }
        if ($zhuanye) {
            $where[] = ['zhuanye', 'like', '%' . $zhuanye . '%'];
        }
        if ($class) {
            $where[] = ['class', 'like', '%' . $class . '%'];
        }
        if ($grade) {
            $where[] = ['grade', 'like', '%' . $grade . '%'];
        }
        if ($start_and_end) {
            $start = isset($start_and_end[0]) ? strtotime($start_and_end[0]) : '';
            $end = isset($start_and_end[1]) ? strtotime($start_and_end[1]) : '';
            $where[] = ['s.create_time', '>=', $start];
            $where[] = ['s.create_time', '<=', $end];
        }
        $list = Db::name('Sign')->alias('s')->where($where)
            ->join('member m', 's.uid = m.uid')->page($page, $limit)
            ->field('m.uid,m.username,m.zhuanye,m.grade,m.class,m.student_no,s.id,s.title,s.address,s.create_time')
            ->order('s.id desc')
            ->select();
        foreach ($list as $k => $v) {
            $list[$k]['grade'] = model('member')::GRADE[$v['grade']];
            $list[$k]['class'] = Db::name('banji')->where('id', $v['class'])->value('title');
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        $total = Db::name('Sign')->alias('s')->where($where)->join('member m', 's.uid = m.uid')->count();
        $data = [
            'list' => $list,
            'total' => $total,
            'grade' => model('member')::GRADE
        ];
        return $this->ok($data);
    }

    public function detail()
    {
        $id = request()->param('id', 0, 'intval');
        if (!$id) {
            return $this->fail('参数错误');
        }
        $detail = Db::name('Sign')->find($id);
        if (empty($detail)) {
            return $this->fail('查无该学生签到信息');
        }
        $user = model('Member')->where('uid', $detail['uid'])->find();
        $detail['username'] = $user['username'];
        $detail['zhuanye'] = $user['zhuanye'];
        $detail['student_no'] = $user['student_no'];
        $detail['grade'] = model('member')::GRADE[$user['grade']];
        $detail['class'] = Db::name('banji')->where('id', $user['class'])->value('title');
        $detail['create_time'] = date('Y-m-d H:i:s', $detail['create_time']);
        return $this->ok($detail);
    }

    public function delete()
    {
        $id = request()->param('id', '');
        $res = Db::name('sign')->where('id', 'in', $id)->delete();
        if ($res) {
            return $this->ok('删除成功');
        } else {
            return $this->fail('删除失败');
        }
    }

    public function save()
    {
        $params = request()->only(['title', 'id', 'uid']);
        if (!isset($params['id'])) {
            return $this->fail('参数错误');
        }
        $res = Db::name('sign')->where(['id' => $params['id'], 'uid' => $params['uid']])->update($params);
        if ($res >= 0) {
            return $this->ok('修改成功');
        } else {
            return $this->fail('修改失败');
        }

    }
}